package com.example.controller;

import com.example.common.Result;
import com.example.entity.TodoList;
import com.example.service.ListService;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/todolist")
public class ListController {

    @Resource
    private ListService listService;

    // 新增
    @PostMapping
    //requestbody将接受的信息转化为类
    public Result add(@RequestBody TodoList list) {
        listService.save(list);
        return Result.success();
    }

    // 修改
    @PutMapping
    public Result update(@RequestBody TodoList list) {
        listService.save(list);
        return Result.success();
    }

    // 删除
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") Long id) {
        listService.delete(id);
    }

    // 根据id查询
    @GetMapping("/{id}")
    public Result<TodoList> findById(@PathVariable Long id) {
        return Result.success(listService.findById(id));
    }

    // 查询所有
    @GetMapping
    public Result<List<TodoList>> findAll() {
        return Result.success(listService.findAll());
    }

    @GetMapping("/page")
    public Result<Page<TodoList>> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                       @RequestParam(defaultValue = "10") Integer pageSize,
                                       @RequestParam(required = false) String name) {
        return Result.success(listService.findPage(pageNum, pageSize, name));
    }


}
